﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Skyper.Model;
using Skyper.View;
using Skyper.Presenters;

namespace Skyper {
	public partial class ChatPage : PhoneApplicationPage,IChatPageView {
		#region Fields
		public static readonly DependencyProperty MaxItemWidthProperty = DependencyProperty.Register("MaxItemWidth", typeof(Int32), typeof(ChatPage), new PropertyMetadata(480));
		private IList<IChatItemView> chats;
		#endregion
		#region Constructor
		public ChatPage() {
			InitializeComponent();
			this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(ChatPage_OrientationChanged);
			this.pivotControl.SelectionChanged += new SelectionChangedEventHandler(pivotControl_SelectionChanged);
			new ChatPagePresenter(this);
		}
		#endregion
		#region Private
		private void ChatPage_OrientationChanged(object sender, OrientationChangedEventArgs e) {
			this.MaxItemWidth = GetMaxItemWidth(e.Orientation);
		}
		private void pivotControl_SelectionChanged(object sender, SelectionChangedEventArgs e) {
			if (this.SelectedChatChanged != null) {
				this.SelectedChatChanged(sender, new RoutedEventArgs());
			}
		}

		private Int32 GetMaxItemWidth(PageOrientation orientation) {
			switch (orientation) {
				default:
				case PageOrientation.Portrait:
				case PageOrientation.PortraitDown:
				case PageOrientation.PortraitUp:
					return (int)(480 * 0.8);
				case PageOrientation.Landscape:
				case PageOrientation.LandscapeLeft:
				case PageOrientation.LandscapeRight:
					return (int)(800 * 0.8);

			}
		}
		#endregion
		#region Protected
		
		#endregion
		#region Public

		#endregion
		#region Property
		public Int32 MaxItemWidth {
			get { return (Int32)this.GetValue(MaxItemWidthProperty); }
			set { this.SetValue(MaxItemWidthProperty, value); }
		}
		public IList<IChatItemView> Chats {
			get {
				return this.chats;
			}
			set {
				this.chats = value;
				this.pivotControl.ItemsSource = value;
			}
		}
		public IChatItemView ActiveChat {
			get {
				return this.pivotControl.SelectedItem as IChatItemView;
			}
			set {
				this.pivotControl.SelectedItem = value;
			}
		}
		public bool IsBusy {
			get {
				return this.progressBusy.Visibility == System.Windows.Visibility.Visible;
			}
			set {
				this.progressBusy.Visibility = (value ? Visibility.Visible : Visibility.Collapsed);
			}
		}
		#endregion
		#region Events
		public event RoutedEventHandler SelectedChatChanged;
		#endregion
	}
}